package mbp.gui.panels;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;

import mbp.core.Resource;
import mbp.gui.base.IListPanel;
import mbp.message.resource;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ResourceListPanel extends JPanel implements IListPanel
{
	private JTable RcTable;
	private DefaultTableModel RcModel;
	
	private List<resource> rciList;
	private List<Resource> rcList;
	
	private JScrollPane scrollPane2;
	
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * Create the panel.
	 */
	public ResourceListPanel() 
	{
		setLayout(new BorderLayout(0, 0));
		scrollPane2 = new JScrollPane();
		add(scrollPane2);
		
		RcTable = new JTable(){
			@Override
			public boolean isCellEditable(int row, int column) {
		    // set table column uneditable
		    return false;
			}
		  };
		RcTable.setToolTipText("<html>Double click for Resource details<BR>Press DEL to remove</html>");
		  
		
		RcTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		RcTable.setFillsViewportHeight(true);
		scrollPane2.setViewportView(RcTable);
		
		RcTable.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"Resource", "Owner", "UUID"
				}
			));
		RcModel = (DefaultTableModel) RcTable.getModel();
		//RcModel.addTableModelListener(RcTable);
		
		//podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		RcTable.addMouseListener(new MouseAdapter(){
		     public void mouseClicked(MouseEvent e){
		      if (e.getClickCount() == 2){
		         processDoubleClick(RcTable.getSelectedRow());
		         }
		      }
		     } );
		
		RcTable.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_DELETE)
				processKeyPressed(RcTable.getSelectedRow());			
			}
		});

	}
	
	public void processDoubleClick(int index)
	{
		System.out.println(" double click: selected row: " + RcTable.getSelectedRow());
	}
	
	public void processKeyPressed(int index)
	{
		System.out.println(" key pressed: selected row: " + RcTable.getSelectedRow());
	}

	
	private void reloadInfo()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				//int rows = 0;
				RcModel.setRowCount(0);
				
				for(int i=0; i<rciList.size(); i++)
				{
					//RcModel.setRowCount(i);
					String type = rciList.get(i).type;
					String owner = rciList.get(i).owner;
					String uuid = rciList.get(i).resourceid;
					RcModel.insertRow(i, new Object[]{type, owner, uuid});
				}
				
			}
		});
	}
	
	private void reloadObj()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				int rows = 0;
				do
				{
					rows = RcModel.getRowCount();
					if(rows>0)
					{
						RcModel.removeRow(0);
					}
				}while(rows>0);
				Iterator<Resource> it = rcList.iterator();	

				while(it.hasNext())
				{
					Resource rc = it.next();
					String type = rc.getType().getResourceTypeName();
					String owner = rc.getOwner();
					String uuid = rc.getUUID();
					RcModel.insertRow(0, new Object[]{type, owner, uuid});
					RcModel.fireTableDataChanged();
				}			
			}
		});

	}
	
	public JTable getTable()
	{
		return RcTable;
	}
	
	public int getSelectedRow()
	{
		return RcTable.getSelectedRow();
	}

	@Override
	public <resource> void reloadListInfo(List<resource> infoList) {
		this.rciList = (List<mbp.message.resource>) infoList;
		this.rcList = null;
		reloadInfo();
		
	}

	@Override
	public <Resource> void reloadListObj(List<Resource> objList) {
		this.rcList = (List<mbp.core.Resource>) objList;
		this.rciList = null;
		reloadObj();
		
	}

	@Override
	public void reloadList() {
		if(rcList != null)
		{
			reloadObj();
		}
		if(rciList != null)
		{
			reloadInfo();
		}
		
	}

}
